﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Generic;
using HRMSystem.Models;
using System;
using HRMSystem.DBServices;


namespace HRMSystem.Wpf.EmployeePages
{
    /// <summary>
    /// Interaction logic for Employee Contents EmploymentInfo.xaml
    /// </summary>
    public partial class EmploymentInfo : Page
    {
        private static EmploymentInfo instance;
        public IList<string> ErrorList { get; set; }

        private int selectedCompany { get; set; }
        private int selectedBranch { get; set; }

        private EmploymentInfo()
        {
            InitializeComponent();

            EmpmntContractComboBox.ItemsSource = ContractItems.GetList();

            if (Session.AdminState.Equals(0)) //Super Admin
            {
                List<Position> positionList = PositionItems.GetList();
                EmpmntPositionComboBox.ItemsSource = positionList;

                EmpmntCompanyComboBox.IsEnabled = true;
                EmpmntCompanyComboBox.ItemsSource = CompanyService.GetAllCompanies();
                EmpmntCompanyComboBox.Items.Refresh();
            }
            else if (Session.AdminState.Equals(1)) //Company Manager
            {
                List<Position> positionList = PositionItems.GetList();
                positionList.Remove(new Position(1, "Company Manager"));
                EmpmntPositionComboBox.ItemsSource = positionList;

                SelectCompanyTextBlock.Visibility = Visibility.Hidden;
                Company selectedCompany = CompanyService.GetCompanyByCompanyId(Session.CompanyID);
                List<Company> companylist = new List<Company>();
                companylist.Add(selectedCompany);
                EmpmntCompanyComboBox.ItemsSource = companylist;
                EmpmntCompanyComboBox.SelectedItem = selectedCompany;
                EmpmntCompanyComboBox.Items.Refresh();

                EmpmntBranchComboBox.IsEnabled = true;
                EmpmntBranchComboBox.ItemsSource = BranchService.GetAllBranchesByCompanyId(Session.CompanyID);
                EmpmntBranchComboBox.Items.Refresh();
            }
            else if (Session.AdminState.Equals(2)) //Branch Manager
            {
                List<Position> positionList = PositionItems.GetList();
                positionList.Remove(new Position(1, "Company Manager"));
                positionList.Remove(new Position(2, "Branch Manager"));
                EmpmntPositionComboBox.ItemsSource = positionList;

                SelectCompanyTextBlock.Visibility = Visibility.Hidden;
                Company selectedCompany = CompanyService.GetCompanyByCompanyId(Session.CompanyID);
                List<Company> companylist = new List<Company>();
                companylist.Add(selectedCompany);
                EmpmntCompanyComboBox.ItemsSource = companylist;
                EmpmntCompanyComboBox.SelectedItem = selectedCompany;
                EmpmntCompanyComboBox.Items.Refresh();

                SelectBranchTextBlock.Visibility = Visibility.Hidden;
                Branch selectedBranch = BranchService.GetBranchByBranchId(Session.BranchID);
                List<Branch> branchlist = new List<Branch>();
                branchlist.Add(selectedBranch);
                EmpmntBranchComboBox.ItemsSource = branchlist;
                EmpmntBranchComboBox.SelectedItem = selectedBranch;
                EmpmntBranchComboBox.Items.Refresh();

                EmpmntDepartmentComboBox.IsEnabled = true;
                EmpmntDepartmentComboBox.ItemsSource = DepartmentService.GetDepartmentsByBranchId(Session.BranchID);
                EmpmntDepartmentComboBox.Items.Refresh();
            }
            else if (Session.AdminState.Equals(3)) //Department Manager
            {
                List<Position> positionList = PositionItems.GetList();
                positionList.Remove(new Position(1, "Company Manager"));
                positionList.Remove(new Position(2, "Branch Manager"));
                positionList.Remove(new Position(3, "Department Manager"));
                EmpmntPositionComboBox.ItemsSource = positionList;

                SelectCompanyTextBlock.Visibility = Visibility.Hidden;
                Company selectedCompany = CompanyService.GetCompanyByCompanyId(Session.CompanyID);
                List<Company> companylist = new List<Company>();
                companylist.Add(selectedCompany);
                EmpmntCompanyComboBox.ItemsSource = companylist;
                EmpmntCompanyComboBox.SelectedItem = selectedCompany;
                EmpmntCompanyComboBox.Items.Refresh();

                SelectBranchTextBlock.Visibility = Visibility.Hidden;
                Branch selectedBranch = BranchService.GetBranchByBranchId(Session.BranchID);
                List<Branch> branchlist = new List<Branch>();
                branchlist.Add(selectedBranch);
                EmpmntBranchComboBox.ItemsSource = branchlist;
                EmpmntBranchComboBox.SelectedItem = selectedBranch;
                EmpmntBranchComboBox.Items.Refresh();

                SelectDepartmentTextBlock.Visibility = Visibility.Hidden;
                Department selectedDepartment = DepartmentService.GetDepartmentByDepartmentId(Session.DepartmentID);
                List<Department> departmentlist = new List<Department>();
                departmentlist.Add(selectedDepartment);
                EmpmntDepartmentComboBox.ItemsSource = departmentlist;
                EmpmntDepartmentComboBox.SelectedItem = selectedDepartment;
                EmpmntDepartmentComboBox.Items.Refresh();
            }
        }

        public static EmploymentInfo Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EmploymentInfo();
                }

                return instance;
            }
        }

        public Title GetEmploymentDetails()
        {
            try
            {
                Title title = new Title();
                Employee employee = new Employee();

                if (PasswordTextBox.Text.Equals(RePasswordTextBox.Text))
                {
                    employee.PASSWORD = PasswordTextBox.Text;
                    employee.USERNAME = UserNameTextBox.Text;
                }

                title.com_employees = employee;

                title.POSITION = (EmpmntPositionComboBox.SelectedItem as Position).ID;
                title.DEPARTMENT_ID = (EmpmntDepartmentComboBox.SelectedItem as Department).ID;
                title.FROMDATE = EmpmntFromDatePicker.SelectedDate;
                title.TODATE = EmpmntToDatePicker.SelectedDate;
                title.POST = EmpTitleTextBox.Text;
                title.CONTRACT = (EmpmntContractComboBox.SelectedItem as Contract).ID;

                //if (EmpBasicSalaryTextBox.Text != "")
                //    title.BASIC_SALARY = float.Parse(EmpBasicSalaryTextBox.Text);

                //if (EmpNetSalaryTextBox.Text != "")
                //    title.NET_SALARY = float.Parse(EmpNetSalaryTextBox.Text);


                //if (EmpAllowanceTravelTextBox.Text != "")
                //    title.ALLOWANCE1 = float.Parse(EmpAllowanceTravelTextBox.Text);

                //if (EmpAllowance2TextBox.Text != "")
                //    title.ALLOWANCE2 = float.Parse(EmpAllowance2TextBox.Text);

                //if (EmpAllowanceOtherTextBox.Text != "")
                //    title.ALLOWANCE3 = float.Parse(EmpAllowanceOtherTextBox.Text);

                //if (EmpDeductionLoanTextBox.Text != "")
                //    title.DEDUC_LOAN = float.Parse(EmpDeductionLoanTextBox.Text);

                //if (EmpDeductionPhoneTextBox.Text != "")
                //    title.DEDUC_PHONE = float.Parse(EmpDeductionPhoneTextBox.Text);

                //if (EmpDeductionOtherTextBox.Text != "")
                //    title.DEDUC_OTHER = float.Parse(EmpDeductionOtherTextBox.Text);

                //if (EmpLeaveAnnualTextBox.Text != "")
                //    title.LVE_ANNUAL = int.Parse(EmpLeaveAnnualTextBox.Text);

                //if (EmpLeaveCasualTextBox.Text != "")
                //    title.LVE_COMPASSIONATE = int.Parse(EmpLeaveCasualTextBox.Text);

                //if (EmpLeaveMedicalTextBox.Text != "")
                //    title.LVE_MEDICAL = int.Parse(EmpLeaveMedicalTextBox.Text);


                title.BASIC_SALARY = EmpBasicSalaryTextBox.Text != "" ? float.Parse(EmpBasicSalaryTextBox.Text) : 0;
                title.NET_SALARY = EmpNetSalaryTextBox.Text != "" ? float.Parse(EmpNetSalaryTextBox.Text) : 0;
                title.ALLOWANCE1 = EmpAllowanceTravelTextBox.Text != "" ? float.Parse(EmpAllowanceTravelTextBox.Text) : 0;
                title.ALLOWANCE2 = EmpAllowance2TextBox.Text != "" ? float.Parse(EmpAllowance2TextBox.Text) : 0;
                title.ALLOWANCE3 = EmpAllowanceOtherTextBox.Text != "" ? float.Parse(EmpAllowanceOtherTextBox.Text) : 0;
                title.DEDUC_LOAN = EmpDeductionLoanTextBox.Text != "" ? float.Parse(EmpDeductionLoanTextBox.Text) : 0;
                title.DEDUC_PHONE = EmpDeductionPhoneTextBox.Text != "" ? float.Parse(EmpDeductionPhoneTextBox.Text) : 0;
                title.DEDUC_OTHER = EmpDeductionOtherTextBox.Text != "" ? float.Parse(EmpDeductionOtherTextBox.Text) : 0;
                title.LVE_ANNUAL = EmpLeaveAnnualTextBox.Text != "" ? int.Parse(EmpLeaveAnnualTextBox.Text) : 0;
                title.LVE_COMPASSIONATE = EmpLeaveCasualTextBox.Text != "" ? int.Parse(EmpLeaveCasualTextBox.Text) : 0;
                title.LVE_MEDICAL = EmpLeaveMedicalTextBox.Text != "" ? int.Parse(EmpLeaveMedicalTextBox.Text) : 0;
                title.COMMENT = EmpTitleCommentTextBox.Text;

                title.EMPLOYEE_ID = Session.NewEmployeeID;
                title.ISCURRENT = true;
                title.INSERT_DATETIME = DateTime.Now;
                title.INSERT_USER_ID = Session.LoggedEmployee.ID;
                title.UPDATE_DATETIME = DateTime.Now;
                title.UPDATE_USER_ID = Session.LoggedEmployee.ID;

                return title;
            }
            catch (Exception)
            {
                return null;
            }
        }


        private void EmpmntPositionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectPositionTextBlock.Visibility = EmpmntPositionComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;

            var comboBox = sender as ComboBox;
            Position pos = EmpmntPositionComboBox.SelectedItem as Position;

            //If not a Manager
            if (pos.ID == 0 || pos.ID == 4)
            {
                UserNamePaswordPanel.Height = 0;
            }
            else
            {
                UserNamePaswordPanel.Height = Double.NaN;
            }
        }

        private void EmpmntCompanyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectCompanyTextBlock.Visibility = EmpmntCompanyComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;

            SelectBranchTextBlock.Visibility = Visibility.Visible;
            EmpmntBranchComboBox.IsEnabled = false;
            EmpmntBranchComboBox.ItemsSource = null;
            EmpmntBranchComboBox.Items.Refresh();

            SelectDepartmentTextBlock.Visibility = Visibility.Visible;
            EmpmntDepartmentComboBox.IsEnabled = false;
            EmpmntDepartmentComboBox.ItemsSource = null;
            EmpmntDepartmentComboBox.Items.Refresh();

            if(EmpmntCompanyComboBox.SelectedItem != null)
            {
                EmpmntBranchComboBox.IsEnabled = true;
                EmpmntBranchComboBox.ItemsSource = BranchService.GetAllBranchesByCompanyId(int.Parse(EmpmntCompanyComboBox.SelectedValue.ToString()));
                EmpmntBranchComboBox.Items.Refresh();
            }
        }

        private void EmpmntBranchComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectBranchTextBlock.Visibility = EmpmntBranchComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;

            SelectDepartmentTextBlock.Visibility = Visibility.Visible;
            EmpmntDepartmentComboBox.IsEnabled = false;
            EmpmntDepartmentComboBox.ItemsSource = null;
            EmpmntDepartmentComboBox.Items.Refresh();

            if (EmpmntBranchComboBox.SelectedItem != null)
            {
                EmpmntDepartmentComboBox.IsEnabled = true;
                EmpmntDepartmentComboBox.ItemsSource = DepartmentService.GetDepartmentsByBranchId(int.Parse(EmpmntBranchComboBox.SelectedValue.ToString()));
                EmpmntDepartmentComboBox.Items.Refresh();
            }
        }

        private void EmpmntDepartmentComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectDepartmentTextBlock.Visibility = EmpmntDepartmentComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
        }

        private void EmpmntContractComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectContractTextBlock.Visibility = EmpmntContractComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
        }

        private void TwoDecimalPoint_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = ValidateUtility.SetDecimalPoints(sender, e, 2);
        }
    }
}
